package org.taishan.electronshop.servlet;

import org.taishan.electronshop.dao.CustomerDao;
import org.taishan.electronshop.dao.GoodsDao;
import org.taishan.electronshop.dao.OrderDao;
import org.taishan.electronshop.dao.TrolleyDao;
import org.taishan.electronshop.domain.Customer;
import org.taishan.electronshop.domain.Order;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/orderitems/*")
public class OrderItemsServlet extends HttpServlet {
    private TrolleyDao td = new TrolleyDao();
    private GoodsDao gd = new GoodsDao();
    private CustomerDao cd = new CustomerDao();
    private OrderDao od = new OrderDao();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        /*换成int报错空指针异常 */
        Integer userid = (Integer) session.getAttribute("userid");
        System.out.println(userid);
        Customer c = (Customer) session.getAttribute("customer");
//        userid = c.getId();
//        session.setAttribute("userid", userid);
        if (username == null || username.length() <= 0) {
            response.sendRedirect("/index.vm");
            return;
        }

        String uri = request.getRequestURI(); // 从 请求对象中获取 URI
        System.out.println("URI : " + uri);
        /**
         * uri : /employee           ========>  firstSlash == lastSlash
         * uri : /employee/         ========>   lastSlash == length -1
         * uri : /employee/xiaowen
         * uri : /employee/hezi/qing
         */
        int firstSlash = uri.indexOf("/"); // 获得第一个 / 对应的索引
        int lastSlash = uri.lastIndexOf("/"); // 获得最后一个 / 对应的索引
        int length = uri.length(); // 获得 字符串的长度
        int dotIndex = uri.lastIndexOf("."); // 从 uri 中获得最后一个 圆点的 索引

        // 如果最后一个斜杠不是第一个斜杠、并且最后一个斜杠不是最后一个字符、并且没有圆点
        if (firstSlash != lastSlash && lastSlash != length - 1 && dotIndex == -1) {
            String m = uri.substring(lastSlash + 1); // 截取 最后一个斜杠之后的内容
            System.out.println("最后一个 / 之后的内容:  " + m);
            m = m.toLowerCase(); // 全部转小写
            // JDK 1.5  之前 switch 中允许使用 byte 、short 、char、int ，从JDK 1.5 开始支持 枚举
            switch (m) { // Java 7 开始支持在  switch 中使用 String
                case "order-back":
                    orderBack(request, response);
                    break;
                default:
                    break;
            }
        }
    }

    private void orderConfirm(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        Order o = (Order) session.getAttribute("noworder");
        o.setState("待收货");
        o.setOperate("收货");
        session.removeAttribute("noworder");
        session.removeAttribute("noworderitems");
        od.update(o);
        response.sendRedirect("/order/order-alllist");
        return;
    }

    private void orderBack(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        Order o = (Order) session.getAttribute("noworder");
        o.setState("待收货");
        o.setOperate("收货");
        session.removeAttribute("noworder");
        session.removeAttribute("noworderitems");
        od.update(o);
        response.sendRedirect("/order/order-alllist");
        return;
    }

    private void orderCancel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        Order o = (Order) session.getAttribute("noworder");
        o.setState("订单已取消");
        o.setOperate(null);
        session.removeAttribute("noworder");
        session.removeAttribute("noworderitems");
        od.update(o);
        response.sendRedirect("/trolley/trolley-items-list");
        return;
    }


}

